package com.fang.algorithm.zuo.linked;

import java.util.Objects;

/**
 * @author fanglingxiao
 * @version 1.0
 * @description 单链表模拟队列
 * @date 2022/4/23 12:28 上午
 **/
public class Test04_LinkedListMockStack {

    public static class Node<T> {
        private T value;
        private Node<T> next;

        public Node(T value) {
            this.value = value;
        }
    }

    public static class Stack<T> {
        private int size;
        private Node<T> head;

        public Stack() {
            this.size = 0;
            head = null;
        }

        public void push(T data) {
            Node<T> dataNode = new Node<>(data);
            if (head == null) {
                head = dataNode;
            } else {
                dataNode.next = head;
                head = dataNode;
            }
            size++;
            System.out.print(data + " ");
        }

        public T pop() {
            T data = null;
            if (head != null) {
                data = head.value;
                head = head.next;
                size--;
            }
            System.out.print(Objects.isNull(data) ? "" : data + " ");
            return data;
        }
    }

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < 10; i++) {
            stack.push(i);
        }
        System.out.println();
        while (true) {
            if (null == stack.pop()) {
                return;
            }
        }
    }
}
